
package harlequin;

import java.sql.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.io.*;
import javax.swing.JOptionPane;
import jxl.write.WriteException;

public class ReportWithoutIncentive extends javax.swing.JFrame {

    private Connection connector;
    private String date;
    private String section;
    private List<String> idNumber;
    private List<String> name;
    private BufferedWriter output;
    
    public ReportWithoutIncentive(Connection connector) throws IOException {
        
        this.connector = connector;
        this.setLocationByPlatform(rootPaneCheckingEnabled);
        idNumber = new ArrayList<String>();
        name = new ArrayList<String>();
        this.setTitle("HTG Report");
        initComponents();
        initializeShowCombo();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bGenerate = new javax.swing.JButton();
        bBack = new javax.swing.JButton();
        cDateTimeslot = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        bGenerate.setText("Generate Report");
        bGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGenerateActionPerformed(evt);
            }
        });

        bBack.setText("Back");
        bBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBackActionPerformed(evt);
            }
        });

        cDateTimeslot.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Date - Timeslot" }));
        cDateTimeslot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cDateTimeslotActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bBack, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addComponent(bGenerate, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addComponent(cDateTimeslot, 0, 226, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cDateTimeslot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 145, Short.MAX_VALUE)
                .addComponent(bGenerate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bBack)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean InvalidDate(String date) throws SQLException{
    
        
    String query = "select * from attendance";
    PreparedStatement preparedStatement = connector.prepareStatement(query);
    ResultSet set = preparedStatement.executeQuery(query);
    
    while(set.next()){
        if(set.getString("playdate").equals(date)) 
            return true;
    }
    
    return false;
}

    public boolean PlayChecker(String date, String section) throws SQLException{
    String query = "select * from attendance";
    PreparedStatement preparedStatement = connector.prepareStatement(query);
    
   ResultSet set = preparedStatement.executeQuery(query);
    
    while(set.next()){
        if(set.getString("playdate").equals(date) && set.getString("section").equals(section))
            return true;
    }
    
    return false;
}

    public boolean SectionExsists(String section) throws SQLException{
   
    String query = "select * from attendance";
   PreparedStatement preparedStatement = connector.prepareStatement(query);
   ResultSet set = preparedStatement.executeQuery(query);
    
    while(set.next()){
        if(set.getString("section").equals(section)) 
            return true;
    }
    
    return false;
}
    
    
    
private void bGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGenerateActionPerformed

   if(cDateTimeslot.getSelectedItem() != "Select Date - Timeslot"){
   try {
     Statement stmt = connector.createStatement();
     ResultSet rs = stmt.executeQuery("Select  idNumber, fullName from attendancedlsu where dateTimeslot = \"" + date + "\" and withIncentive = 0");
      while(rs.next()){
       idNumber.add(rs.getString("idNumber"));
       name.add(rs.getString("fullName"));
      }
      } catch (SQLException ex) {
          Logger.getLogger(ReportWithoutIncentive.class.getName()).log(Level.SEVERE, null, ex);
      }
        WriteExcel test = new WriteExcel();
		        String dummystring = System.getProperty("user.home")+"\\Desktop";
       System.out.println(dummystring);
		test.setOutputFile(dummystring+"\\StudentWithoutIncentiveReport.xls");
                    try {
            try {
                test.write(idNumber, name,date,section);
            } catch (IOException ex) {
                Logger.getLogger(ReportWithoutIncentive.class.getName()).log(Level.SEVERE, null, ex);
            }
                    } catch (WriteException ex) {
                        Logger.getLogger(ReportWithoutIncentive.class.getName()).log(Level.SEVERE, null, ex);
                    }
		System.out.println("Please check the result file dektop ");
                this.dispose();
   }
   else
       JOptionPane.showMessageDialog(null, "Please Select Date - Timeslot first!");
   
}//GEN-LAST:event_bGenerateActionPerformed

private void initializeShowCombo(){
    
    try {
  Statement stmt = connector.createStatement();
  ResultSet rs = stmt.executeQuery("Select * from play");
  while(rs.next()){
    cDateTimeslot.addItem(rs.getString("dateTimeslot"));
  }
  } catch (SQLException ex) {
      Logger.getLogger(HomePage.class.getName()).log(Level.SEVERE, null, ex);
  }   
}

private void bBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBackActionPerformed
    this.dispose();
}//GEN-LAST:event_bBackActionPerformed

private void cDateTimeslotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cDateTimeslotActionPerformed
    date = cDateTimeslot.getSelectedItem().toString();
}//GEN-LAST:event_cDateTimeslotActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBack;
    private javax.swing.JButton bGenerate;
    private javax.swing.JComboBox cDateTimeslot;
    // End of variables declaration//GEN-END:variables
}
